package com.mybed.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mybed.crm.anno.Log;
import com.mybed.crm.mapper.OperationMapper;
import com.mybed.crm.pojo.dto.PageOperationDto;
import com.mybed.crm.pojo.pojo.Operation;
import com.mybed.crm.result.PageResult;
import com.mybed.crm.result.ResultP;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/monitor")
@Slf4j
//系统日志
public class MonitorController {
    @Autowired
    private OperationMapper operationMapper;

    /**
     * 日志分页查询
     * @param dto
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/operlog/list")
    public PageResult operlogList(PageOperationDto dto,
                                  @RequestParam("params[beginTime]")String beginTime,
                                  @RequestParam("params[endTime]")String endTime){
        LambdaQueryWrapper<Operation> wrapper=new LambdaQueryWrapper<>();
        if(StringUtils.hasLength(dto.getKeyWord())){
            wrapper.like(Operation::getTitle,dto.getKeyWord());
        }
        if(StringUtils.hasLength(beginTime)&&StringUtils.hasLength(endTime)){
            wrapper.between(Operation::getOperTime,beginTime,endTime);
        }
        IPage<Operation> page=new Page<>(dto.getPageNum(),dto.getPageSize());
        IPage<Operation> iPage = operationMapper.selectPage(page, wrapper);
        PageResult result=new PageResult();
        result.insertData(iPage);
        return result;
    }

    /**
     * 清空所有操作日志
     * @return
     */
    @Log("清空所有操作日志")
    @DeleteMapping("/operlog/clean")
    public ResultP clean(){
        operationMapper.delete(null);
        return ResultP.success();
    }

    /**
     * 批量删除日志
     * @param ids
     * @return
     */
    @Log("批量删除日志")
    @DeleteMapping("/operlog/{ids}")
    public ResultP cleanByIds(@PathVariable String ids){
        List<String> list = Arrays.asList(ids.split(","));
        operationMapper.deleteByIds(list);
        return ResultP.success();
    }
}
